package cn.tedu.yushougeproduct.service.impl;


import cn.tedu.yushouge.commons.ex.ServiceException;
import cn.tedu.yushouge.commons.web.ServiceCode;
import cn.tedu.yushougeproduct.mapper.OrdersMapper;
import cn.tedu.yushougeproduct.pojo.entity.Category;
import cn.tedu.yushougeproduct.pojo.entity.Orders;
import cn.tedu.yushougeproduct.pojo.param.OrdersAddNewParam;
import cn.tedu.yushougeproduct.pojo.vo.OrdersListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.OrdersStandardVO;
import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushougeproduct.service.IOrdersService;
import cn.tedu.yushouge.commons.util.PageInfoToPageDataConverter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class OrdersServiceImpl implements IOrdersService {

    public OrdersServiceImpl() {
        log.info("创建业务对象：OrdersServiceImpl");
    }

    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据ID删除订单】的业务，参数：{}", id);
        // 检查商品分类是否存在，如果不存在，则抛出异常
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = ordersMapper.deleteById(id);
        if (countById == 0) {
            String message = "删除订单失败，订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        log.debug("从订单表删除一条数据，完成！");
    }

    @Override
    public void updateOrderState(Long orderId, Long stateId) {
        log.debug("开始处理【根据ID修改订单状态】的业务，购物车id：{}，状态id：{}", orderId, stateId);
        ordersMapper.updateOrderState(orderId, stateId);

        Orders orders = new Orders();
        orders.setId(orderId);
        orders.setGmtModified(LocalDateTime.now());
        ordersMapper.updateById(orders);
    }

    @Override
    public OrdersStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询订单详情】的业务，参数：{}", id);
        OrdersStandardVO queryResult = ordersMapper.getStandardById(id);
        if (queryResult == null){
            String message = "查询订单详情失败，订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<OrdersListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<OrdersListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询订单列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<OrdersListItemVO> list = ordersMapper.getOrdersList();
        PageInfo<OrdersListItemVO> pageInfo = new PageInfo<>(list);
        PageData<OrdersListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    @Override
    public List<OrdersListItemVO> listByUserId(Long userId) {
        log.debug("开始处理【根据用户id查询订单列表】的业务，用户id:{}",userId);
        return ordersMapper.listByUserId(userId);
    }

}
